﻿using Prism.Ioc;
using SqlSugar;
using System.Collections.Generic;
using System.Configuration;

namespace LaserAutoTest.UI.Extensions
{
    public static class SqlSugarExtension
    {
        public static void RegisterSqlSugar(this IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterSingleton<SqlSugarClient>(() => GetInstance());
        }

        private static SqlSugarClient GetInstance()
        {
            List<ConnectionConfig> lstDbConfigs = new List<ConnectionConfig>();

            var mysqlConnection = ConfigurationManager.ConnectionStrings["laserAutoTest"].ConnectionString;
            //var initConnection = ConfigurationManager.ConnectionStrings["InitConnection"].ConnectionString;
            //var sqlLite = $"Data Source={AppDomain.CurrentDomain.SetupInformation.ApplicationBase}datatable.db";


            lstDbConfigs.Add(new ConnectionConfig()
            {
                ConfigId = "laserAutoTest",
                DbType = DbType.MySql,
                ConnectionString = mysqlConnection,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    DataInfoCacheService = new HttpRuntimeCache() //配置我们创建的缓存类
                }
            });

            //lstDbConfigs.Add(new ConnectionConfig()
            //{
            //    ConfigId = "InitConnection",
            //    DbType = DbType.MySql,
            //    ConnectionString = initConnection,
            //    IsAutoCloseConnection = true,
            //    InitKeyType = InitKeyType.Attribute,
            //    ConfigureExternalServices = new ConfigureExternalServices()
            //    {
            //        DataInfoCacheService = new HttpRuntimeCache() //配置我们创建的缓存类
            //    }
            //});

            //lstDbConfigs.Add(new ConnectionConfig()
            //{
            //    ConfigId = "sqlLite",
            //    DbType = DbType.Sqlite,
            //    ConnectionString = sqlLite,
            //    IsAutoCloseConnection = true,
            //    InitKeyType = InitKeyType.Attribute,
            //    ConfigureExternalServices = new ConfigureExternalServices()
            //    {
            //        DataInfoCacheService = new HttpRuntimeCache() //配置我们创建的缓存类
            //    }
            //});
            return new SqlSugarClient(lstDbConfigs);
        }
    }
}
